package org.example.game.history;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/05/12 10:37
 */
public class Solution100274 {

    public static void main(String[] args) {
        Solution100274 solution100274 = new Solution100274();

        int[] energy = new int[] {-2,-3,-1};
        int k = 2;
        System.out.println(solution100274.maximumEnergy3(energy, k));
    }

    public int maximumEnergy3(int[] energy, int k) {
        //倒过来计算起始值
        int[] dp = new int[energy.length];
        for(int i=energy.length-1;i>=0;i--){
            if(i+k >= energy.length){
                dp[i] = energy[i];
            }else {
                dp[i] = energy[i]+dp[i+k];
            }
        }

        int max = dp[0];
        for(int i=1;i<dp.length;i++){
            if(dp[i] > max){
                max = dp[i];
            }
        }
        return max;
    }

    public int maximumEnergy2(int[] energy, int k) {

        int[] dp = new int[energy.length];
        for(int i=0;i<energy.length;i++){
            for(int j=i;j<energy.length;j++){
                if((j-i) % k == 0){
                    dp[i] += energy[j];
                }
            }
        }

        int max = dp[0];
        for(int i=1;i<dp.length;i++){
            if(dp[i] > max){
                max = dp[i];
            }
        }
        return max;

    }

    public int maximumEnergy(int[] energy, int k) {

        if(energy.length < k){
            int max = 0;
            for(int i=0;i<energy.length;i++){
                if(energy[i] > max){
                    max = energy[i];
                }
            }
            return max;
        }


        int start = 0;
        int startValue = energy[0];
        for(int i=0;i<energy.length;i++){
            if(energy[i] > 0){
                start = i;
                break;
            }else if(energy[i] > startValue){
                start = i;
                startValue = energy[i];
            }
        }

        int sum = 0;

        if(start + 1 >= energy.length){
            sum += energy[energy.length-1];
            return sum;
        }

        for(int i=start;i<energy.length;i++){

            if(i+1 >= energy.length){
                if(energy[i] > 0){
                    sum += energy[i];
                }
            }else if (i+k >= energy.length) {
                if( (energy[i]) > (energy[i+1])){
                    sum += energy[i] + energy[i+k];
                    i = i+k;
                }
            }else if(i+k+1 >= energy.length){
                if( (energy[i] + energy[i+k]) > (energy[i+1])){
                    sum += energy[i] + energy[i+k];
                    i = i+k;
                }
            }else {
                if( (energy[i] + energy[i+k]) > (energy[i+1] + energy[i+1+k])){
                    sum += energy[i] + energy[i+k];
                    i = i+k;
                }
            }
        }

        return sum;
    }

}
